/**
 * 数据源初始化
 */
var SdsDataSource = {
    id: "SdsDataSourceTable",	//表格id
    seItem: null,		//选中的条目
    table: null,
    ztree: null,
    layerIndex: -1,
    parentId:0
};
SdsDataSource.refresh = function () {
    SdsDataSource.table.refresh();
    SdsDataSource.ztree.init();
}
// Dept.refresh =function(){
//     Dept.table.refresh();
//     Dept.ztree.init();
// };
/**
 * 初始化表格的列
 */
SdsDataSource.initColumn = function () {
    return [
        {field: 'selectItem', radio: true},
        {title: '数据源名称', field: 'sourceDesc', align: 'center', width: '300px', valign: 'middle', sortable: true},
        {title: '数据源代码', field: 'sourceCode', align: 'center', width: '350px', valign: 'middle', sortable: true},
        {title: '是否有效', field: 'isActive', align: 'center', valign: 'middle', sortable: true, formatter:function(value,row,index) {
                if (value == 1) {
                    return "<span class='label label-success'>是</span>";
                } else {
                    return "<span class='label label-warning'>否</span>";
                }
            }},
        {title: '备注', field: 'resume', align: 'center',width:'400px', valign: 'middle', sortable: true},
        {title: '操作', field: 'id', align: 'center', width: '100px', valign: 'middle',formatter:function(value,row,index){
                var _html = '<a href="javascript:SdsDataSource.edit(\''+row.id+'\')" title="修改"><i class="hisui-icon icon-write-order"></i></a>';
                // _html += '<a href="javascript:SdsDataSource.del(\''+row.id+'\',\''+row.name+'\')" title="删除"><i class="hisui-icon icon-cancel"></i></a>';
                return _html;
            }}];
};

/**
 * 检查是否选中
 */
SdsDataSource.check = function () {
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    if(selected.length == 0){
        Sys.info("请先选中表格中的某一记录！");
        return false;
    }else{
        SdsDataSource.seItem = selected[0];
        return true;
    }
};

/**
 * 点击新增
 */
SdsDataSource.add = function () {
     // this.layerIndex = Sys.open('新增数据源',Sys.backendPath + '/sds/sdsstanddatasource/add',['520px','400px']);
     var index = Sys.open('新增数据源',Sys.backendPath + '/sds/sdsstanddatasource/add',['50%','58%']);
     this.layerIndex = index;
};

/**
 * 查看数据源详情
 */
SdsDataSource.edit = function (id) {
    var flag = false;
    if(id){
        flag = true;
    }else if (this.check()) {
        id = this.seItem.id;
        flag = true;
    }
    if (flag) {
        this.layerIndex = Sys.open('修改数据源',Sys.backendPath + '/sds/sdsstanddatasource/to_update/' + id,['50%','58%']);
    }
};

/**
 * 删除数据源
 */
SdsDataSource.del = function (id,name) {
    var flag = false;
    if(id){
        flag = true;
    }else if (this.check()) {
        id = this.seItem.id;
        name = this.seItem.name
        flag = true;
    }
    if (flag) {
        var operation = function(){
            var ajax = new $ax(Sys.backendPath + "/sds/sdsstanddatasource/delete", function (data) {
                Sys.success("删除成功!");
                SdsDataSource.table.refresh();
            }, function (data) {
                Sys.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("sdsStandDataSourceId",id);
            ajax.start();
        };
        Sys.confirm("是否删除数据源?",operation);
    }
};

/**
 * 查询数据源
 */
SdsDataSource.search = function () {
    var queryData = {};
    queryData['parentId'] = '';
    queryData['sourceDesc'] = $("#condition").val();
    SdsDataSource.table.refresh({query: queryData});
};
/**
 * 根据数据源tree改变表格内容
 */
SdsDataSource.reselect = function (){
    var queryData = {};
    queryData['parentId'] = SdsDataSource.parentId;
    queryData['sourceDesc'] = null;
    SdsDataSource.table.refresh({query: queryData});
};

SdsDataSource.onClickData = function (e, treeId, treeNode) {
    SdsDataSource.parentId = treeNode.id;
    SdsDataSource.reselect();
};

SdsDataSource.contrast = function (){
    var index = Sys.open('对照标化字典',Sys.backendPath + '/sds/sdsstanddatacontrast/');
    this.layerIndex = index;
}

SdsDataSource.expData = function () {
    var $df = $("#downForm");
    //提交表单，实现下载
    $df.submit();
};

SdsDataSource.impData = function () {
    if((SdsDataSource.parentId==0)||(SdsDataSource.parentId==''))
    {
        Sys.info("请先选中要导入组织机构树的节点！");
        return false;
    }
    var index = layer.open({
        type: 2,
        title: '导入数据源',
        area: ['550px', '300px'], //宽高
        fix: false, //不固定
        maxmin: true,
        content: Sys.backendPath + '/sds/sdsstanddatasource/to_import/'+ SdsDataSource.parentId
    });
    this.layerIndex = index;
};

// Dept.template = function () {
//     window.location.href = Sys.backendPath+"/static/backend/system/dept/科室导入模板.xlsx";
// };

$(function () {

    var defaultColunms = SdsDataSource.initColumn();
    var table = new BSTable(SdsDataSource.id, Sys.backendPath + "/sds/sdsstanddatasource/list", defaultColunms);
    table.setPaginationType("server");
    table.setSubHeight(-10);
    table.setPageSize(10);
    // table.setSort("weight");
    table.init();
    SdsDataSource.table = table;


    var ztree = new $ZTree("datasourceTree", Sys.backendPath + "/sds/sdsstanddatasource/tree");
    ztree.bindOnClick(SdsDataSource.onClickData);
    ztree.init();
    SdsDataSource.ztree = ztree;
    fuzzySearch('datasourceTree','#seachKey',null,true);
});
